package ua.com.jpy.controllers.home;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import ua.com.jpy.entity.customer.Customer;
import ua.com.jpy.services.customer.ICustomerService;
import ua.com.jpy.services.product.ProductService;

/**
 * @author LSD25
 *
 */
@Controller
@SessionAttributes({ "customer" })
@RequestMapping(value = { "/*" })
public class HomeController {
	
	private static final Log log = LogFactory.getLog(HomeController.class);
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private ICustomerService customerService;

	@SuppressWarnings("deprecation")
	@RequestMapping(value = {"*", ""}, method = RequestMethod.GET)
	public String homeController(ModelMap modelMap) {
		Customer customer = null;
		if(modelMap.containsKey("customer") && modelMap.get("customer") instanceof Customer) {
			customer = (Customer) modelMap.get("customer");
		} else {
			customer = customerService.getCurrentCustomer();
		}
		if(customer != null) {
			log.info("Customer found, login: " + customer.getLogin());
			modelMap.addAttribute(customer);
			modelMap.addObject(customer);
		} else {
			log.info("Customer was not found, add new instance customer to the session");
		}
		log.info("homeController");
		return "home";
	}
	
}